package de.le.mfw.client.main.presenter;

import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.events.HasTabSelectedHandlers;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

import de.le.mfw.client.main.MainEventBus;
import de.le.mfw.client.main.view.MainViewImpl;
import de.le.mfw.client.main.view.widget.TabSetWithPermission.Permission;
import de.le.mfw.shared.UserDTO;

@Presenter(view = MainViewImpl.class)
public class MainPresenter extends BasePresenter<MainViewImpl, MainEventBus> {
	public interface MainView extends IsWidget {
		public void setUserWidget(Widget userWidget);

		public void setContent(Widget content);

		public void setUserRole(Permission userRole);

		public HasTabSelectedHandlers getMenu();

		public Tab getSettingsTab();
	}

	private UserDTO user = null;

	@Override
	public void bind() {
		view.getMenu().addTabSelectedHandler(new MenuClickHandler());
	}

	public void onStart() {

	}

	public void onLogin(UserDTO user) {
		this.user = user;

		view.setUserRole(Permission.LoggedIn);
	}

	public void onLogout() {
		this.user = null;

		view.setUserRole(Permission.None);
	}

	public void onSetUserWidget(Widget userWidget) {
		view.setUserWidget(userWidget);
	}

	public void onSetContentWidget(Widget contentWidget) {
		view.setContent(contentWidget);
	}

	private final class MenuClickHandler implements TabSelectedHandler {
		public void onTabSelected(TabSelectedEvent event) {
			Tab tab = event.getTab();

			if (tab == view.getSettingsTab())
				eventBus.goToSetting(user);
		}
	}

}